package com.ruoyi.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.hotel.domain.HmRoom;
import com.ruoyi.hotel.domain.HmRoomType;
import com.ruoyi.hotel.mapper.HmRoomMapper;
import com.ruoyi.hotel.mapper.HmRoomTypeMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.domain.bo.HmBookBo;
import com.ruoyi.hotel.domain.vo.HmBookVo;
import com.ruoyi.hotel.domain.HmBook;
import com.ruoyi.hotel.mapper.HmBookMapper;
import com.ruoyi.hotel.service.IHmBookService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@RequiredArgsConstructor
@Service
public class HmBookServiceImpl implements IHmBookService {

    private final HmBookMapper baseMapper;
    private final HmRoomMapper hmRoomMapper;
    private final HmRoomTypeMapper hmRoomTypeMapper;

    /**
     * 查询单个预约
     */
    @Override
    public HmBookVo queryById(Long bookId){
        return baseMapper.selectVoById(bookId);
    }

    /**
     * 查询所有预约列表(page)
     */
    @Override
    public TableDataInfo<HmBookVo> queryPageList(HmBookBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmBook> lqw = buildQueryWrapper(bo);
        Page<HmBookVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询所有预约列表
     */
    @Override
    public List<HmBookVo> queryList(HmBookBo bo) {
        LambdaQueryWrapper<HmBook> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmBook> buildQueryWrapper(HmBookBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmBook> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCustomerId() != null, HmBook::getCustomerId, bo.getCustomerId());
        lqw.eq(bo.getCheckinTime() != null, HmBook::getCheckinTime, bo.getCheckinTime());
        lqw.eq(bo.getCheckoutTime() != null, HmBook::getCheckoutTime, bo.getCheckoutTime());
        lqw.eq(bo.getRoomId() != null, HmBook::getRoomId, bo.getRoomId());
        lqw.eq(bo.getIsExist() != null, HmBook::getIsExist, bo.getIsExist());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceRequestContent()), HmBook::getServiceRequestContent, bo.getServiceRequestContent());
        return lqw;
    }

    /**
     * 新增预约
     */
    @Override
    public Boolean insertByBo(HmBookBo bo) {
        HmBook add = BeanUtil.toBean(bo, HmBook.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBookId(add.getBookId());
        }
        return flag;
    }

    /**
     * 修改预约
     */
    @Override
    public Boolean updateByBo(HmBookBo bo) {
        HmBook update = BeanUtil.toBean(bo, HmBook.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public int updateById(HmBook bo) {
        return baseMapper.updateById(bo);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmBook entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean checkBook(Long id){
        QueryWrapper<HmBook> qw=new QueryWrapper<>();
        qw.eq("customer_id",id).eq("is_exist","y");
        return baseMapper.exists(qw);
    }

    @Override
    public List<HmBook> getMyBook(Long id){
        QueryWrapper<HmBook> qw=new QueryWrapper<>();
        qw.eq("customer_id",id).eq("is_exist","y");
        List<HmBook> list=baseMapper.selectList(qw);
        return list;
    }


    /**
     * 取消预约
     */
    @Override
    public void cancelBook(HmBook book){

        book.setIsExist("q");
        baseMapper.updateById(book);

        QueryWrapper<HmRoom> qw=new QueryWrapper<>();
        qw.eq("room_id",book.getRoomId());
        HmRoom hmRoom=hmRoomMapper.selectOne(qw);
        hmRoom.setRoomStatus("f");
        hmRoomMapper.update(hmRoom,qw);

        QueryWrapper<HmRoomType> qw3=new QueryWrapper<>();
        qw3.eq("room_type",hmRoom.getRoomType());
        HmRoomType hmRoomType=hmRoomTypeMapper.selectOne(qw3);
        Long temp=hmRoomType.getRoomCount()+1;
        hmRoomType.setRoomCount(temp);
        hmRoomTypeMapper.update(hmRoomType,qw3);

    }

}
